package cn.com.dcsgo.receiver;

import cn.com.dcsgo.constant.MqConst;
import cn.com.dcsgo.service.SkuService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

/**
 * SkuReceiver
 *
 * @author Dcsgo
 * @since 2025/4/18 下午4:40
 */
@Component
@RequiredArgsConstructor
public class SkuReceiver {
    private final SkuService skuService;

    /**
     * 监听商品上架消息
     *
     * @param skuId   SKU ID
     * @param message 消息
     * @param channel 通道
     * @throws IOException IO异常
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
            key = {MqConst.ROUTING_GOODS_UPPER}
    ))
    public void upperSku(Long skuId, Message message, Channel channel) throws IOException {
        if (skuId != null) {
            //ES中添加SKU相关信息
            skuService.insertById(skuId);
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 监听商品下架消息
     *
     * @param skuId   SKU ID
     * @param message 消息
     * @param channel 通道
     * @throws IOException IO异常
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
            key = {MqConst.ROUTING_GOODS_LOWER}
    ))
    public void lowerSku(Long skuId, Message message, Channel channel) throws IOException {
        if (skuId != null) {
            //移除ES中相关SKU信息
            skuService.deleteById(skuId);
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 监听商品删除消息
     * @param skuId SKU ID
     * @param message 消息
     * @param channel 通道
     * @throws IOException IO异常
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_DELETE, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
            key = {MqConst.ROUTING_GOODS_DELETE}
    ))
    public void deleteSku(Long skuId, Message message, Channel channel) throws IOException {
        if (skuId != null) {
            //移除ES中相关SKU信息
            skuService.deleteById(skuId);
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 监听商品批量删除消息
     * @param skuIds SKU ID
     * @param message 消息
     * @param channel 通道
     * @throws IOException IO异常
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_BATCH_DELETE, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
            key = {MqConst.ROUTING_GOODS_BATCH_DELETE}
    ))
    public void batchDeleteSku(List<Long> skuIds, Message message, Channel channel) throws IOException {
        if (!CollectionUtils.isEmpty(skuIds)) {
            //移除ES中相关SKU信息
            skuService.deleteByIds(skuIds);
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
